package com.xinchen.gufeiwulian.ui.adapter

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.maple.model.data.MainMenuData
import com.maple.utils.ext.layoutInflater
import com.maple.utils.util.UIUtils

class MainMenuAdapter(val context: Context): RecyclerView.Adapter<RecyclerView.ViewHolder>(){

    companion object {
        const val TYPE_HEADER: Int = 1
        const val TYPE_LIST: Int = 0
    }

    private val list: MutableList<MainMenuData> = mutableListOf()

    private var listener: OnClickListener? = null

    fun setListener(l: OnClickListener?) {
        listener = l
    }

    fun setData(l: List<MainMenuData>?) {
        if(!l.isNullOrEmpty()) {
            this.list.clear()
            this.list.addAll(l)
            this.notifyDataSetChanged()
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if(viewType == TYPE_HEADER) {
            val view: View = context.layoutInflater.inflate(com.maple.res.R.layout.item_main_header, parent, false)
            val holder: HeaderViewHolder = HeaderViewHolder(view)
            return holder
        } else {
            val view: View = context.layoutInflater.inflate(com.maple.res.R.layout.item_main_menu, parent, false)
            val holder: ListViewHolder = ListViewHolder(view)
            holder.itemRoot.setOnClickListener {
                listener?.onItemClick(
                    holder.bindingAdapterPosition - 1,
                    list.get(holder.bindingAdapterPosition - 1),
                )
            }
            return holder
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if(holder is HeaderViewHolder) {
            holder.setTitle()
        } else if(holder is ListViewHolder) {
            holder.setData(position - 1,list.get(position - 1))
        }
    }

    override fun getItemCount(): Int = list.size + 1


    override fun getItemViewType(position: Int): Int {
        return if(position == 0) return TYPE_HEADER else TYPE_LIST
    }

    inner class HeaderViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
        private val tvTitle: TextView = itemView.findViewById(com.maple.res.R.id.tv_title)
        fun setTitle() {
            tvTitle.text = "业务办理"
        }
    }

    inner class ListViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
        val itemRoot: LinearLayout = itemView.findViewById(com.maple.res.R.id.item_root)
        private val tvName: TextView = itemView.findViewById(com.maple.res.R.id.tv_name)
        private val ivIcon: ImageView = itemView.findViewById(com.maple.res.R.id.iv_icon)
        fun setData(position: Int,data: MainMenuData) {
            tvName.text = data.name
            ivIcon.background = UIUtils.getDrawable(data.icon)
        }

    }

    interface OnClickListener {
        fun onItemClick(pos: Int = 0, item: MainMenuData?)
    }
}